;*******************************************************************************
; Title:	FileTypes
; Author:	Original by Paul Clifford
;		Modified by Philip Ludlam
; Copyright:	(C) Paul Clifford 2002
; Version:	1.02, 7 May 2006
;
; Changes:	Updated for the A9home by Philip Ludlam
;
; Purpose: Produces a Director menu containing a list of filetypes
;
; Syntax: *FileTypes [types to be included]
; eg...
;	*FileTypes 0		User filetypes		&000-&0FF
;	*FileTypes 321		Public domain filetypes	&100-&3FF
;	*FileTypes DCB987654	Commercial filetypes	&400-&9FF and &B00-&DFF
;	*FileTypes A		Acorn filetypes		&A00-&AFF
;	*FileTypes FE		Generic filetypes	&E00-&FFF
;	*FileTypes		All filetypes		&000-&FFF
;
;
;This program is free software; you can redistribute it and/or modify it under
;the terms of the GNU General Public License as published by the Free Software
;Foundation; either version 2 of the License, or (at your option) any later
;version.
;
;This program is distributed in the hope that it will be useful, but WITHOUT ANY
;WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
;PARTICULAR PURPOSE. See the GNU General Public License for more details.
;
;You should have received a copy of the GNU General Public License along with
;this program; if not, write to the Free Software Foundation, Inc., 59 Temple
;Place - Suite 330, Boston, MA 02111-1307, USA
;
;*******************************************************************************


		TTL	> FileTypes

		GET	OSLib:oslib.hdr.OS
		GET	AsmLib2:hdr.RegsBoth
		GET	AsmLib2:hdr.MacrosBoth

Director_Menu		*	&4A100
XDirector_Menu		*	&6A100
Director_EndMenu	*	&4A101
XDirector_EndMenu	*	&6A101
Director_Option		*	&4A102
XDirector_Option	*	&6A102
Director_Dash		*	&4A103
XDirector_Dash		*	&6A103
Director_Command	*	&4A104
XDirector_Command	*	&6A104


		AREA	|main|, CODE, READONLY

		ENTRY

;-------------------------------------------------------------------------------

Start		B	Entry			; Entry instruction
		DCD	&79766748		; Magic value 1
		DCD	&216C6776		; Magic value 2
		DCD	Data-Start		; Read only size
		DCD	End-Data		; Read/write size
		DCD	32			; 26 or 32 bit build

;-------------------------------------------------------------------------------

Entry		MOV	r11, lr			; Save return address
		MOV	r10, r0			; Parameters string

Find_Parameters
		LDRB	r0, [r10], #1
		CMP	r0, #32
		BGT	Find_Parameters
		ADRLT	r10, All_FileTypes	; If no parameters, supply a default set
		BLT	Generate_Menu

Parameters_Supplied
		LDRB	r0, [r10], #1
		CMP	r0, #32
		BEQ	Parameters_Supplied	; Skip any extra spaces
		ADRLT	r10, All_FileTypes	; If no parameters, supply a default set
		SUBGT	r10, r10, #1

Generate_Menu
		ADR	r0, Set_Director_Menu
		SWI	XOS_CLI			; Make sure Director knows which menu to open
		MOVVS	pc, r11			; exit if error
		ADR	r0, Menu_Name
		SWI	XDirector_Menu		; Begins creation of the menu
		MOVVS	pc, r11			; exit if error

Generate_Menu_Loop
		LDRB	r0, [r10], #1
		CMP	r0, #32
		BLT	Generate_Menu_Loop_lt
		STRB	r0, FileType_Number
		BL	Add_FileTypes
		B	Generate_Menu_Loop

Generate_Menu_Loop_lt
		ADR	r0, Sort_Menu		; Entries should be sorted
		SWI	XDirector_EndMenu	; Finishes the menu
		MOV	pc, r11


Add_FileTypes
		MOV	r3, #0
		ADR	r1, Option_Text
Add_FileTypes_Loop
		ADR	r0, FileType_Variable
		MOV	r2, #8
		MOV	r4, #0
		SWI	XOS_ReadVarVal
		CMP	r2, #0
		MOVEQ	pc, lr
		MOV	r4, #34
		STRB	r4, [r1, r2]
		MOV	r4, #32
		ADD	r2, r2, #1
Insert_Spaces
		CMP	r2, #9
		STRLTB	r4, [r1, r2]
		ADDLT	r2, r2, #1
		BLT	Insert_Spaces
		LDRB	r4, [r3, #10]
		STRB	r4, Option_Number
		STRB	r4, Command_Number
		LDRB	r4, [r3, #11]
		STRB	r4, Option_Number + 1
		STRB	r4, Command_Number + 1
		LDRB	r4, [r3, #12]
		STRB	r4, Option_Number + 2
		STRB	r4, Command_Number + 2
		ADR	r0, Option
		SWI	XDirector_Option		; Name of menu entry
		MOVVS	pc, r11			; exit if error
		ADR	r0, Command
		SWI	XDirector_Command	; Command to be executed if entry is chosen
		MOVVS	pc, r11			; exit if error
		B	Add_FileTypes_Loop

		ALIGN
Data

All_FileTypes
		=	"#", 0
Set_Director_Menu
		=	"Set Director$Menu FileTypes", 0
Menu_Name
		=	"<Director$Menu> -temp", 0
Sort_Menu
		=	"-sort", 0

FileType_Variable
		=	"File$Type_"
FileType_Number
		=	"###", 0

Option
		=	34
Option_Text
		=	"########", 34
		=	" -sprite small_"
Option_Number
		=	"###", 0
Command
		=	"SetType <Path> "
Command_Number
		=	"###", 0

		ALIGN
End
		END
